home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / RCS.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  44KB  |  1,430 lines

  1. /*
  2.  *                      RCS create/change operation
  3.  */
  4. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  5.    Copyright 1990 by Paul Eggert
  6.    Distributed under license by the Free Software Foundation, Inc.
  7.  
  8. This file is part of RCS.
  9.  
  10. RCS is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 1, or (at your option)
  13. any later version.
  14.  
  15. RCS is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with RCS; see the file COPYING.  If not, write to
  22. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. Report problems and direct all questions to:
  25.  
  26.     rcs-bugs@cs.purdue.edu
  27.  
  28. */
  29.  
  30.  
  31.  
  32.  
  33. /* $Log: rcs.c%v $
  34.  * Revision 1.2  1991/08/23  13:32:15  SGP
  35.  * Ported to MSDOS using Borland C++
  36.  *
  37.  * Revision 5.7  1990/12/18  17:19:21  eggert
  38.  * Fix bug with multiple -n and -N options.
  39.  *
  40.  * Revision 5.6  1990/12/04  05:18:40  eggert
  41.  * Use -I for prompts and -q for diagnostics.
  42.  *
  43.  * Revision 5.5  1990/11/11  00:06:35  eggert
  44.  * Fix `rcs -e' core dump.
  45.  *
  46.  * Revision 5.4  1990/11/01  05:03:33  eggert
  47.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  48.  *
  49.  * Revision 5.3  1990/10/04  06:30:16  eggert
  50.  * Accumulate exit status across files.
  51.  *
  52.  * Revision 5.2  1990/09/04  08:02:17  eggert
  53.  * Standardize yes-or-no procedure.
  54.  *
  55.  * Revision 5.1  1990/08/29  07:13:51  eggert
  56.  * Remove unused setuid support.  Clean old log messages too.
  57.  *
  58.  * Revision 5.0  1990/08/22  08:12:42  eggert
  59.  * Don't lose names when applying -a option to multiple files.
  60.  * Remove compile-time limits; use malloc instead.  Add setuid support.
  61.  * Permit dates past 1999/12/31.  Make lock and temp files faster and safer.
  62.  * Ansify and Posixate.  Add -V.  Fix umask bug.  Make linting easier.  Tune.
  63.  * Yield proper exit status.  Check diff's output.
  64.  *
  65.  * Revision 4.11  89/05/01  15:12:06  narten
  66.  * changed copyright header to reflect current distribution rules
  67.  * 
  68.  * Revision 4.10  88/11/08  16:01:54  narten
  69.  * didn't install previous patch correctly
  70.  * 
  71.  * Revision 4.9  88/11/08  13:56:01  narten
  72.  * removed include <sysexits.h> (not needed)
  73.  * minor fix for -A option
  74.  * 
  75.  * Revision 4.8  88/08/09  19:12:27  eggert
  76.  * Don't access freed storage.
  77.  * Use execv(), not system(); yield proper exit status; remove lint.
  78.  * 
  79.  * Revision 4.7  87/12/18  11:37:17  narten
  80.  * lint cleanups (Guy Harris)
  81.  * 
  82.  * Revision 4.6  87/10/18  10:28:48  narten
  83.  * Updating verison numbers. Changes relative to 1.1 are actually 
  84.  * relative to 4.3
  85.  * 
  86.  * Revision 1.4  87/09/24  13:58:52  narten
  87.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  88.  * warnings)
  89.  * 
  90.  * Revision 1.3  87/03/27  14:21:55  jenkins
  91.  * Port to suns
  92.  * 
  93.  * Revision 1.2  85/12/17  13:59:09  albitz
  94.  * Changed setstate to rcs_setstate because of conflict with random.o.
  95.  * 
  96.  * Revision 4.3  83/12/15  12:27:33  wft
  97.  * rcs -u now breaks most recent lock if it can't find a lock by the caller.
  98.  * 
  99.  * Revision 4.2  83/12/05  10:18:20  wft
  100.  * Added conditional compilation for sending mail.
  101.  * Alternatives: V4_2BSD, V6, USG, and other.
  102.  * 
  103.  * Revision 4.1  83/05/10  16:43:02  wft
  104.  * Simplified breaklock(); added calls to findlock() and getcaller().
  105.  * Added option -b (default branch). Updated -s and -w for -b.
  106.  * Removed calls to stat(); now done by pairfilenames().
  107.  * Replaced most catchints() calls with restoreints().
  108.  * Removed check for exit status of delivermail().
  109.  * Directed all interactive output to stderr.
  110.  * 
  111.  * Revision 3.9.1.1  83/12/02  22:08:51  wft
  112.  * Added conditional compilation for 4.2 sendmail and 4.1 delivermail.
  113.  * 
  114.  * Revision 3.9  83/02/15  15:38:39  wft
  115.  * Added call to fastcopy() to copy remainder of RCS file.
  116.  *
  117.  * Revision 3.8  83/01/18  17:37:51  wft
  118.  * Changed sendmail(): now uses delivermail, and asks whether to break the lock.
  119.  *
  120.  * Revision 3.7  83/01/15  18:04:25  wft
  121.  * Removed putree(); replaced with puttree() in rcssyn.c.
  122.  * Combined putdellog() and scanlogtext(); deleted putdellog().
  123.  * Cleaned up diagnostics and error messages. Fixed problem with
  124.  * mutilated files in case of deletions in 2 files in a single command.
  125.  * Changed marking of selector from 'D' to DELETE.
  126.  *
  127.  * Revision 3.6  83/01/14  15:37:31  wft
  128.  * Added ignoring of interrupts while new RCS file is renamed;
  129.  * Avoids deletion of RCS files by interrupts.
  130.  *
  131.  * Revision 3.5  82/12/10  21:11:39  wft
  132.  * Removed unused variables, fixed checking of return code from diff,
  133.  * introduced variant COMPAT2 for skipping Suffix on -A files.
  134.  *
  135.  * Revision 3.4  82/12/04  13:18:20  wft
  136.  * Replaced getdelta() with gettree(), changed breaklock to update
  137.  * field lockedby, added some diagnostics.
  138.  *
  139.  * Revision 3.3  82/12/03  17:08:04  wft
  140.  * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
  141.  * /usr/ucb/Mail with macro MAIL. Removed handling of Suffix (-x).
  142.  * fixed -u for missing revno. Disambiguated structure members.
  143.  *
  144.  * Revision 3.2  82/10/18  21:05:07  wft
  145.  * rcs -i now generates a file mode given by the umask minus write permission;
  146.  * otherwise, rcs keeps the mode, but removes write permission.
  147.  * I added a check for write error, fixed call to getlogin(), replaced
  148.  * curdir() with getfullRCSname(), cleaned up handling -U/L, and changed
  149.  * conflicting, long identifiers.
  150.  *
  151.  * Revision 3.1  82/10/13  16:11:07  wft
  152.  * fixed type of variables receiving from getc() (char -> int).
  153.  */
  154.  
  155.  
  156. #include "rcsbase.h"
  157.  
  158. struct  Lockrev {
  159.     const char *revno;
  160.         struct  Lockrev   * nextrev;
  161. };
  162.  
  163. struct  Symrev {
  164.     const char *revno;
  165.     const char *ssymbol;
  166.         int     override;
  167.         struct  Symrev  * nextsym;
  168. };
  169.  
  170. struct  Status {
  171.     const char *revno;
  172.     const char *status;
  173.         struct  Status  * nextstatus;
  174. };
  175.  
  176. enum changeaccess {append, erase};
  177. struct chaccess {
  178.     const char *login;
  179.     enum changeaccess command;
  180.     struct chaccess *nextchaccess;
  181. };
  182.  
  183. struct delrevpair {
  184.     const char *strt;
  185.     const char *end;
  186.         int     code;
  187. };
  188.  
  189. static int buildeltatext P((const struct hshentries*));
  190. static int removerevs P((void));
  191. static int sendmail P((const char*,const char*));
  192. static struct Lockrev *rmnewlocklst P((const struct Lockrev*));
  193. static void breaklock P((const struct hshentry*));
  194. static void buildtree P((void));
  195. static void cleanup P((void));
  196. static void getaccessor P((char*,enum changeaccess));
  197. static void getassoclst P((int,char*));
  198. static void getchaccess P((const char*,enum changeaccess));
  199. static void getdelrev P((char*));
  200. static void getstates P((char*));
  201. static void rcs_setstate P((const char*,const char*));
  202. static void scanlogtext P((struct hshentry*,int));
  203. static void setlock P((const char*));
  204. static void updateaccess P((void));
  205. static void updateassoc P((void));
  206. static void updatelocks P((void));
  207.  
  208. static struct buf numrev;
  209. static const char *headstate;
  210. static int chgheadstate, exitstatus, lockhead, unlockcaller;
  211. static struct Lockrev *newlocklst, *rmvlocklst;
  212. static struct Status *statelst, *laststate;
  213. static struct Symrev *assoclst, *lastassoc;
  214. static struct chaccess *chaccess, **nextchaccess;
  215. static struct delrevpair delrev;
  216. static struct hshentry *cuthead, *cuttail, *delstrt;
  217. static struct hshentries *gendeltas;
  218.  
  219. mainProg(rcsId, "rcs", "$Id: rcs.c%v 1.2 1991/08/23 13:32:15 SGP Exp $")
  220. {
  221.     static const char cmdusage[] =
  222.         "\nrcs usage: rcs -alogins -Aoldfile -{blu}[rev] -cstring -e[logins] -i -{LU} -{nN}name[:rev] -orange -sstate[:rev] -t[textfile] -Vn file ...";
  223.  
  224.     const char *branchsym, *commsyml, *textfile;
  225.     int branchflag, expmode, initflag;
  226.     int r, strictlock, strict_selected, textflag;
  227.     mode_t defaultRCSmode;    /* default mode for new RCS files */
  228.     struct buf branchnum;
  229.         struct  Lockrev *curlock,  * rmvlock, *lockpt;
  230.         struct  Status  * curstate;
  231.  
  232.     initid();
  233.     catchints();
  234.  
  235.     nextchaccess = &chaccess;
  236.     branchsym = commsyml = textfile = nil;
  237.     branchflag = strictlock = false;
  238.     bufautobegin(&branchnum);
  239.     curlock = rmvlock = nil;
  240.     defaultRCSmode = S_IREAD;
  241.     expmode = -1;
  242.         initflag= textflag = false;
  243.         strict_selected = 0;
  244.  
  245.         /*  preprocessing command options    */
  246.         while (--argc,++argv, argc>=1 && ((*argv)[0] == '-')) {
  247.                 switch ((*argv)[1]) {
  248.  
  249.         case 'i':   /*  initial version  */
  250.                         initflag = true;
  251.                         break;
  252.  
  253.                 case 'b':  /* change default branch */
  254.             if (branchflag) redefined('b');
  255.                         branchflag= true;
  256.                         branchsym = (*argv)+2;
  257.                         break;
  258.  
  259.                 case 'c':   /*  change comment symbol   */
  260.             if (commsyml) redefined('c');
  261.                         commsyml = (*argv)+2;
  262.                         break;
  263.  
  264.                 case 'a':  /*  add new accessor   */
  265.             getaccessor(*argv+1, append);
  266.                         break;
  267.  
  268.                 case 'A':  /*  append access list according to accessfile  */
  269.             *argv += 2;
  270.             if (!**argv) {
  271.                 error("missing file name after -A");
  272.                             break;
  273.                         }
  274.             if (0 < pairfilenames(1,argv,rcsreadopen,true,false)) {
  275.                 while (AccessList) {
  276.                 getchaccess(strsave(AccessList->login), append);
  277.                 AccessList = AccessList->nextaccess;
  278.                 }
  279.                 ffclose(finptr);
  280.                         }
  281.                         break;
  282.  
  283.                 case 'e':    /*  remove accessors   */
  284.             getaccessor(*argv+1, erase);
  285.                         break;
  286.  
  287.                 case 'l':    /*   lock a revision if it is unlocked   */
  288.                         if ( (*argv)[2] == '\0'){ /* lock head or def. branch */
  289.                             lockhead = true;
  290.                             break;
  291.                         }
  292.             lockpt = talloc(struct Lockrev);
  293.                         lockpt->revno = (*argv)+2;
  294.                         lockpt->nextrev = nil;
  295.                         if ( curlock )
  296.                             curlock->nextrev = lockpt;
  297.                         else
  298.                             newlocklst = lockpt;
  299.                         curlock = lockpt;
  300.                         break;
  301.  
  302.                 case 'u':   /*  release lock of a locked revision   */
  303.                         if ( (*argv)[2] == '\0'){ /*  unlock head  */
  304.                             unlockcaller=true;
  305.                             break;
  306.                         }
  307.             lockpt = talloc(struct Lockrev);
  308.                         lockpt->revno = (*argv)+2;
  309.                         lockpt->nextrev = nil;
  310.                         if (rmvlock)
  311.                             rmvlock->nextrev = lockpt;
  312.                         else
  313.                             rmvlocklst = lockpt;
  314.                         rmvlock = lockpt;
  315.  
  316.                         curlock = rmnewlocklst(lockpt);
  317.                         break;
  318.  
  319.                 case 'L':   /*  set strict locking */
  320.                         if (strict_selected++) {  /* Already selected L or U? */
  321.                if (!strictlock)      /* Already selected -U? */
  322.                    warn("-L overrides -U.");
  323.                         }
  324.                         strictlock = true;
  325.                         break;
  326.  
  327.                 case 'U':   /*  release strict locking */
  328.                         if (strict_selected++) {  /* Already selected L or U? */
  329.                if (strictlock)      /* Already selected -L? */
  330.                    warn("-L overrides -U.");
  331.                         }
  332.             else
  333.                 strictlock = false;
  334.                         break;
  335.  
  336.                 case 'n':    /*  add new association: error, if name exists */
  337.                         if ( (*argv)[2] == '\0') {
  338.                 error("missing symbolic name after -n");
  339.                             break;
  340.                         }
  341.                         getassoclst(false, (*argv)+1);
  342.                         break;
  343.  
  344.                 case 'N':   /*  add or change association   */
  345.                         if ( (*argv)[2] == '\0') {
  346.                 error("missing symbolic name after -N");
  347.                             break;
  348.                         }
  349.                         getassoclst(true, (*argv)+1);
  350.                         break;
  351.  
  352.         case 'o':   /*  delete revisions  */
  353.             if (delrev.strt) redefined('o');
  354.                         if ( (*argv)[2] == '\0' ) {
  355.                 error("missing revision range after -o");
  356.                             break;
  357.                         }
  358.                         getdelrev( (*argv)+1 );
  359.                         break;
  360.  
  361.                 case 's':   /*  change state attribute of a revision  */
  362.                         if ( (*argv)[2] == '\0') {
  363.                 error("state missing after -s");
  364.                             break;
  365.                         }
  366.                         getstates( (*argv)+1);
  367.                         break;
  368.  
  369.                 case 't':   /*  change descriptive text   */
  370.                         textflag=true;
  371.                         if ((*argv)[2]!='\0'){
  372.                 if (textfile) redefined('t');
  373.                                 textfile = (*argv)+2;
  374.                         }
  375.                         break;
  376.  
  377.         case 'I':
  378.             interactiveflag = true;
  379.             break;
  380.  
  381.                 case 'q':
  382.                         quietflag = true;
  383.                         break;
  384.  
  385.         case 'V':
  386.             setRCSversion(*argv);
  387.             break;
  388.  
  389.         case 'k':    /*  set keyword expand mode  */
  390.             if (0 <= expmode) redefined('k');
  391.             if (0 <= (expmode = str2expmode(*argv+2)))
  392.                 break;
  393.             /* fall into */
  394.                 default:
  395.             faterror("unknown option: %s%s", *argv, cmdusage);
  396.                 };
  397.         }  /* end processing of options */
  398.  
  399.     if (argc<1) faterror("no input file%s", cmdusage);
  400.         if (nerror) {
  401.         diagnose("%s aborted\n",cmdid);
  402.         exitmain(EXIT_FAILURE);
  403.         }
  404. #ifdef NOTDEF
  405.     if (initflag) {
  406.         defaultRCSmode = umask((mode_t)0);
  407.         VOID umask(defaultRCSmode);
  408.         defaultRCSmode = ~defaultRCSmode & 0444;
  409.     }
  410. #endif
  411.  
  412.         /* now handle all filenames */
  413.         do {
  414.     foutptr = NULL;
  415.         finptr=frewrite=NULL;
  416.     ffree();
  417.  
  418.         if ( initflag ) {
  419.         switch (pairfilenames(argc, argv, rcswriteopen, false, false)) {
  420.                 case -1: break;        /*  not exist; ok */
  421.                 case  0: continue;     /*  error         */
  422.                 case  1: error("file %s exists already", RCSfilename);
  423.                          continue;
  424.             }
  425.     }
  426.         else  {
  427.         switch (pairfilenames(argc, argv, rcswriteopen, true, false)) {
  428.                 case -1: continue;    /*  not exist      */
  429.                 case  0: continue;    /*  errors         */
  430.                 case  1: break;       /*  file exists; ok*/
  431.             }
  432.     }
  433.  
  434.  
  435.         /* now RCSfilename contains the name of the RCS file, and
  436.          * workfilename contains the name of the working file.
  437.          * if !initflag, finptr contains the file descriptor for the
  438.          * RCS file. The admin node is initialized.
  439.          */
  440.  
  441.     diagnose("RCS file: %s\n", RCSfilename);
  442.  
  443.     if (initflag && !getworkstat())           continue; /* give up */
  444.     if (!initflag && !checkaccesslist())       continue; /* give up */
  445.  
  446.         gettree(); /* read in delta tree */
  447.  
  448.         /*  update admin. node    */
  449.         if (strict_selected) StrictLocks = strictlock;
  450.     if (commsyml) {
  451.         Comment.string = commsyml;
  452.         Comment.size = strlen(commsyml);
  453.     }
  454.     if (0 <= expmode) Expand = expmode;
  455.  
  456.         /* update default branch */
  457.     if (branchflag && expandsym(branchsym, &branchnum)) {
  458.         if (countnumflds(branchnum.string)) {
  459.         Dbranch = branchnum.string;
  460.             } else
  461.                 Dbranch = nil;
  462.         }
  463.  
  464.     updateaccess();        /*  update access list        */
  465.  
  466.         updateassoc();          /*  update association list   */
  467.  
  468.         updatelocks();          /*  update locks              */
  469.  
  470.         /*  update state attribution  */
  471.         if (chgheadstate) {
  472.             /* change state of default branch or head */
  473.             if (Dbranch==nil) {
  474.                 if (Head==nil)
  475.              warn("can't change states in an empty tree");
  476.                 else Head->state = headstate;
  477.             } else {
  478.         rcs_setstate(Dbranch,headstate); /* Can't set directly */
  479.             }
  480.         }
  481.         curstate = statelst;
  482.         while( curstate ) {
  483.             rcs_setstate(curstate->revno,curstate->status);
  484.             curstate = curstate->nextstatus;
  485.         }
  486.  
  487.         cuthead = cuttail = nil;
  488.     if (delrev.strt && removerevs()) {
  489.             /*  rebuild delta tree if some deltas are deleted   */
  490.             if ( cuttail )
  491.         VOID genrevs(cuttail->num, (char *)nil,(char *)nil,
  492.                  (char *)nil, &gendeltas);
  493.             buildtree();
  494.         }
  495.  
  496.  
  497.         putadmin(frewrite);
  498.         if ( Head )
  499.            puttree(Head, frewrite);
  500.     putdesc(textflag,textfile);
  501.     foutptr = NULL;
  502.  
  503.         if ( Head) {
  504.         if (!delrev.strt) {
  505.                 /* no revision deleted */
  506.                 fastcopy(finptr,frewrite);
  507.             } else {
  508.         if (!cuttail || buildeltatext(gendeltas))
  509.                     scanlogtext((struct hshentry *)nil,nil);
  510.                     /* copy rest of delta text nodes that are not deleted      */
  511.             }
  512.         }
  513.     if (finptr) {ffclose(finptr); finptr=NULL;} /* Help the file system. */
  514.         ffclose(frewrite);   frewrite = NULL;
  515.         if ( ! nerror ) {  /*  move temporary file to RCS file if no error */
  516.         /* update mode */
  517.         seteid();
  518.         r = chmod(newRCSfilename,
  519.              (
  520.                    !initflag ? RCSstat.st_mode
  521.                  : haveworkstat==0 ? workstat.st_mode
  522.                  : defaultRCSmode
  523.              ) & ~(S_IWUSR|S_IWGRP|S_IWOTH)
  524.         );
  525.         if (r == 0) {
  526.         ignoreints();
  527.         r = re_name(newRCSfilename,RCSfilename);
  528.         keepdirtemp(newRCSfilename);
  529.         restoreints();
  530.         }
  531.         setrid();
  532.         if (r != 0) {
  533.         eerror(RCSfilename);
  534.         error("saved in %s", newRCSfilename);
  535.         dirtempunlink();
  536.                 break;
  537.             }
  538.         diagnose("done\n");
  539.         } else {
  540.         diagnose("%s aborted; %s unchanged.\n",cmdid,RCSfilename);
  541.         }
  542.     } while (cleanup(),
  543.                  ++argv, --argc >=1);
  544.  
  545.     tempunlink();
  546.     exitmain(exitstatus);
  547. }       /* end of main (rcs) */
  548.  
  549.     static void
  550. cleanup()
  551. {
  552.     if (nerror) exitstatus = EXIT_FAILURE;
  553.     if (finptr) ffclose(finptr);
  554.     if (frewrite) ffclose(frewrite);
  555.     dirtempunlink();
  556. }
  557.  
  558.     exiting void
  559. exiterr()
  560. {
  561.     dirtempunlink();
  562.     tempunlink();
  563.     _exit(EXIT_FAILURE);
  564. }
  565.  
  566.  
  567.     static void
  568. getassoclst(int flag, char *sp)
  569. /*  Function:   associate a symbolic name to a revision or branch,      */
  570. /*              and store in assoclst                                   */
  571.  
  572. {
  573.         struct   Symrev  * pt;
  574.     const char *temp;
  575.         int                c;
  576.  
  577.         while( (c=(*++sp)) == ' ' || c == '\t' || c =='\n')  ;
  578.         temp = sp;
  579.     sp = checkid(sp, ':');  /*  check for invalid symbolic name  */
  580.     c = *sp;   *sp = '\0';
  581.         while( c == ' ' || c == '\t' || c == '\n')  c = *++sp;
  582.  
  583.         if ( c != ':' && c != '\0') {
  584.         error("invalid string %s after option -n or -N",sp);
  585.             return;
  586.         }
  587.  
  588.     pt = talloc(struct Symrev);
  589.         pt->ssymbol = temp;
  590.         pt->override = flag;
  591.     if (c == '\0')  /*  delete symbol  */
  592.             pt->revno = nil;
  593.         else {
  594.             while( (c = *++sp) == ' ' || c == '\n' || c == '\t')  ;
  595.         if ( c == '\0' )
  596.                 pt->revno = nil;
  597.         else
  598.                 pt->revno = sp;
  599.         }
  600.         pt->nextsym = nil;
  601.         if (lastassoc)
  602.             lastassoc->nextsym = pt;
  603.         else
  604.             assoclst = pt;
  605.         lastassoc = pt;
  606.         return;
  607. }
  608.  
  609.  
  610.     static void
  611. getchaccess(const char *login, enum changeaccess command)
  612. {
  613.     register struct chaccess *pt;
  614.  
  615.     *nextchaccess = pt = talloc(struct chaccess);
  616.     pt->login = login;
  617.     pt->command = command;
  618.     pt->nextchaccess = nil;
  619.     nextchaccess = &pt->nextchaccess;
  620. }
  621.  
  622.  
  623.  
  624.     static void
  625. getaccessor(char *opt, enum changeaccess command)
  626. /*   Function:  get the accessor list of options -e and -a,     */
  627. /*        and store in chaccess                */
  628.  
  629.  
  630. {
  631.         register c;
  632.     register char *sp;
  633.  
  634.     sp = opt;
  635.         while( ( c = *++sp) == ' ' || c == '\n' || c == '\t' || c == ',') ;
  636.         if ( c == '\0') {
  637.         if (command == erase  &&  sp-opt == 1) {
  638.         getchaccess((const char*)nil, command);
  639.         return;
  640.         }
  641.         error("missing login name after option -a or -e");
  642.         return;
  643.         }
  644.  
  645.         while( c != '\0') {
  646.         getchaccess(sp, command);
  647.         sp = checkid(sp,',');
  648.         c = *sp;   *sp = '\0';
  649.                 while( c == ' ' || c == '\n' || c == '\t'|| c == ',')c =(*++sp);
  650.         }
  651. }
  652.  
  653.  
  654.  
  655.     static void
  656. getstates(char *sp)
  657. /*   Function:  get one state attribute and the corresponding   */
  658. /*              revision and store in statelst                  */
  659.  
  660. {
  661.     const char *temp;
  662.         struct  Status  *pt;
  663.         register        c;
  664.  
  665.         while( (c=(*++sp)) ==' ' || c == '\t' || c == '\n')  ;
  666.         temp = sp;
  667.     sp = checkid(sp,':');  /* check for invalid state attribute */
  668.     c = *sp;   *sp = '\0';
  669.         while( c == ' ' || c == '\t' || c == '\n' )  c = *++sp;
  670.  
  671.         if ( c == '\0' ) {  /*  change state of def. branch or Head  */
  672.             chgheadstate = true;
  673.             headstate  = temp;
  674.             return;
  675.         }
  676.         else if ( c != ':' ) {
  677.         error("missing ':' after state in option -s");
  678.             return;
  679.         }
  680.  
  681.         while( (c = *++sp) == ' ' || c == '\t' || c == '\n')  ;
  682.     pt = talloc(struct Status);
  683.         pt->status     = temp;
  684.         pt->revno      = sp;
  685.         pt->nextstatus = nil;
  686.         if (laststate)
  687.             laststate->nextstatus = pt;
  688.         else
  689.             statelst = pt;
  690.         laststate = pt;
  691. }
  692.  
  693.  
  694.  
  695.     static void
  696. getdelrev(char *sp)
  697. /*   Function:  get revision range or branch to be deleted,     */
  698. /*              and place in delrev                             */
  699. {
  700.         int    c;
  701.         struct  delrevpair      *pt;
  702.  
  703.     pt = &delrev;
  704.         while((c = (*++sp)) == ' ' || c == '\n' || c == '\t') ;
  705.  
  706.         if ( c == '<' || c == '-' ) {  /*  -o  -rev  or <rev  */
  707.             while( (c = (*++sp)) == ' ' || c == '\n' || c == '\t')  ;
  708.             pt->strt = sp;    pt->code = 1;
  709.             while( c != ' ' && c != '\n' && c != '\t' && c != '\0') c =(*++sp);
  710.             *sp = '\0';
  711.         pt->end = nil;
  712.             return;
  713.         }
  714.         else {
  715.             pt->strt = sp;
  716.             while( c != ' ' && c != '\n' && c != '\t' && c != '\0'
  717.                    && c != '-' && c != '<' )  c = *++sp;
  718.             *sp = '\0';
  719.             while( c == ' ' || c == '\n' || c == '\t' )  c = *++sp;
  720.             if ( c == '\0' )  {  /*   -o rev or branch   */
  721.                 pt->end = nil;   pt->code = 0;
  722.                 return;
  723.             }
  724.             if ( c != '-' && c != '<') {
  725.         faterror("invalid range %s %s after -o", pt->strt, sp);
  726.             }
  727.             while( (c = *++sp) == ' ' || c == '\n' || c == '\t')  ;
  728.             if ( c == '\0') {  /*  -o   rev-   or   rev<   */
  729.                 pt->end = nil;   pt->code = 2;
  730.                 return;
  731.             }
  732.         }
  733.         /*   -o   rev1-rev2    or   rev1<rev2   */
  734.     pt->end = sp;  pt->code = 3;
  735.         while( c!= ' ' && c != '\n' && c != '\t' && c != '\0') c = *++sp;
  736.         *sp = '\0';
  737. }
  738.  
  739.  
  740.  
  741.  
  742.     static void
  743. scanlogtext(struct hshentry *delta,int edit)
  744. /* Function: Scans delta text nodes up to and including the one given
  745.  * by delta, or up to last one present, if delta==nil.
  746.  * For the one given by delta (if delta!=nil), the log message is saved into
  747.  * curlogmsg and the text is edited if 'edit' is set, copied otherwise.
  748.  * Assumes the initial lexeme must be read in first.
  749.  * Does not advance nexttok after it is finished, except if delta==nil.
  750.  */
  751. {
  752.     const struct hshentry *nextdelta;
  753.     struct cbuf cb;
  754.  
  755.     for (;;) {
  756.         foutptr = NULL;
  757.                 nextlex();
  758.                 if (!(nextdelta=getnum())) {
  759.                     if(delta)
  760.             faterror("can't find delta for revision %s", delta->num);
  761.             if (nexttok != EOFILE)
  762.             fatserror("expecting EOF");
  763.             return; /* no more delta text nodes */
  764.                 }
  765.         if (nextdelta->selector) {
  766.             foutptr = frewrite;
  767.             aprintf(frewrite,DELNUMFORM,nextdelta->num,Klog);
  768.                 }
  769.         getkeystring(Klog);
  770.         if (delta==nextdelta) {
  771.             cb = savestring(&curlogbuf);
  772.             delta->log = curlogmsg =
  773.                 cleanlogmsg(curlogbuf.string, cb.size);
  774.                 } else {readstring();
  775.                 }
  776.                 nextlex();
  777.         while (nexttok==ID && strcmp(NextString,Ktext)!=0)
  778.             ignorephrase();
  779.         getkeystring(Ktext);
  780.  
  781.         if (delta==nextdelta)
  782.             break;
  783.         readstring(); /* skip over it */
  784.  
  785.     }
  786.     /* got the one we're looking for */
  787.     if (edit)
  788.         editstring((struct hshentry *)nil);
  789.     else
  790.         copystring();
  791. }
  792.  
  793.  
  794.  
  795.     static struct Lockrev *
  796. rmnewlocklst(const struct Lockrev *which)
  797. /*   Function:  remove lock to revision which->revno from newlocklst   */
  798.  
  799. {
  800.         struct  Lockrev   * pt, *pre;
  801.  
  802.         while( newlocklst && (! strcmp(newlocklst->revno, which->revno))){
  803.         struct Lockrev *pn = newlocklst->nextrev;
  804.         tfree(newlocklst);
  805.         newlocklst = pn;
  806.         }
  807.  
  808.         pt = pre = newlocklst;
  809.         while( pt ) {
  810.             if ( ! strcmp(pt->revno, which->revno) ) {
  811.                 pre->nextrev = pt->nextrev;
  812.         tfree(pt);
  813.         pt = pre->nextrev;
  814.             }
  815.             else {
  816.                 pre = pt;
  817.                 pt = pt->nextrev;
  818.             }
  819.         }
  820.         return pre;
  821. }
  822.  
  823.  
  824.  
  825.     static void
  826. updateaccess()
  827. {
  828.     register struct chaccess *ch;
  829.     register struct access **p, *t;
  830.  
  831.     for (ch = chaccess;  ch;  ch = ch->nextchaccess) {
  832.         switch (ch->command) {
  833.         case erase:
  834.             if (!ch->login)
  835.                 AccessList = nil;
  836.             else
  837.                 for (p = &AccessList;  (t = *p);  )
  838.                 if (strcmp(ch->login, t->login) == 0)
  839.                     *p = t->nextaccess;
  840.                 else
  841.                     p = &t->nextaccess;
  842.             break;
  843.         case append:
  844.             for (p = &AccessList;  ;  p = &t->nextaccess)
  845.                 if (!(t = *p)) {
  846.                     *p = t = ftalloc(struct access);
  847.                     t->login = ch->login;
  848.                     t->nextaccess = nil;
  849.                     break;
  850.                 } else if (strcmp(ch->login, t->login) == 0)
  851.                     break;
  852.             break;
  853.         }
  854.     }
  855. }
  856.  
  857.  
  858.     static int
  859. sendmail(const char *Delta, const char *who)
  860. /*   Function:  mail to who, informing him that his lock on delta was
  861.  *   broken by caller. Ask first whether to go ahead. Return false on
  862.  *   error or if user decides not to break the lock.
  863.  */
  864. {
  865.     const char *messagefile;
  866.     int old1, old2, c;
  867.         FILE    * mailmess;
  868.  
  869.  
  870.     aprintf(stderr, "Revision %s is already locked by %s.\n", Delta, who);
  871.     if (!yesorno(false, "Do you want to break the lock? [ny](n): "))
  872.         return false;
  873.  
  874.         /* go ahead with breaking  */
  875.     messagefile = maketemp(0);
  876.     errno = 0;
  877.         if ( (mailmess = fopen(messagefile, "w")) == NULL) {
  878.         efaterror(messagefile);
  879.         }
  880.  
  881.     aprintf(mailmess, "Subject: Broken lock on %s\n\nYour lock on revision %s of file %s\nhas been broken by %s for the following reason:\n",
  882.         bindex(RCSfilename,SLASH), Delta, getfullRCSname(), getcaller()
  883.     );
  884.     aputs("State the reason for breaking the lock:\n(terminate with single '.' or end of file)\n>> ", stderr);
  885.  
  886.         old1 = '\n';    old2 = ' ';
  887.         for (; ;) {
  888.         c = getcstdin();
  889.             if ( c == EOF ) {
  890.         aprintf(mailmess, "%c\n", old1);
  891.                 break;
  892.             }
  893.             else if ( c == '\n' && old1 == '.' && old2 == '\n')
  894.                 break;
  895.             else {
  896.         afputc(old1, mailmess);
  897.                 old2 = old1;   old1 = c;
  898.         if (c=='\n') aputs(">> ", stderr);
  899.             }
  900.         }
  901.         ffclose(mailmess);
  902.  
  903.     /* ignore the exit status, even if delivermail unsuccessful */
  904.     VOID run(messagefile, (char*)nil, SENDMAIL, who, (char*)nil);
  905.     return(true);
  906. }
  907.  
  908.  
  909.  
  910.     static void
  911. breaklock(const struct hshentry *delta)
  912. /* function: Finds the lock held by caller on delta,
  913.  * and removes it.
  914.  * Sends mail if a lock different from the caller's is broken.
  915.  * Prints an error message if there is no such lock or error.
  916.  */
  917. {
  918.         register struct lock * next, * trail;
  919.     const char *num;
  920.         struct lock dummy;
  921.  
  922.     num=delta->num;
  923.         dummy.nextlock=next=Locks;
  924.         trail = &dummy;
  925.         while (next!=nil) {
  926.         if (strcmp(num, next->delta->num) == 0) {
  927.             if (
  928.                 strcmp(getcaller(),next->login) != 0
  929.                 &&    !sendmail(num, next->login)
  930.             ) {
  931.                 error("%s still locked by %s", num, next->login);
  932.                 return;
  933.             }
  934.             break; /* exact match */
  935.                 }
  936.                 trail=next;
  937.                 next=next->nextlock;
  938.         }
  939.         if (next!=nil) {
  940.                 /*found one */
  941.         diagnose("%s unlocked\n",next->delta->num);
  942.                 trail->nextlock=next->nextlock;
  943.                 next->delta->lockedby=nil;
  944.                 Locks=dummy.nextlock;
  945.         } else  {
  946.         error("no lock set on revision %s", num);
  947.         }
  948. }
  949.  
  950.  
  951.  
  952.     static struct hshentry *
  953. searchcutpt(const char *object, unsigned length, struct hshentries *store)
  954. /*   Function:  Search store and return entry with number being object. */
  955. /*              cuttail = nil, if the entry is Head; otherwise, cuttail */
  956. /*              is the entry point to the one with number being object  */
  957.  
  958. {
  959.     cuthead = nil;
  960.     while (compartial(store->first->num, object, length)) {
  961.         cuthead = store->first;
  962.         store = store->rest;
  963.     }
  964.     return store->first;
  965. }
  966.  
  967.  
  968.  
  969.     static int
  970. branchpoint(struct hshentry *strt, struct hshentry *tail)
  971. /*   Function: check whether the deltas between strt and tail    */
  972. /*        are locked or branch point, return 1 if any is  */
  973. /*        locked or branch point; otherwise, return 0 and */
  974. /*        mark deleted                    */
  975.  
  976. {
  977.         struct  hshentry    *pt;
  978.     const struct lock *lockpt;
  979.         int     flag;
  980.  
  981.  
  982.         pt = strt;
  983.         flag = false;
  984.         while( pt != tail) {
  985.             if ( pt->branches ){ /*  a branch point  */
  986.                 flag = true;
  987.         error("can't remove branch point %s", pt->num);
  988.             }
  989.         lockpt = Locks;
  990.         while(lockpt && lockpt->delta != pt)
  991.         lockpt = lockpt->nextlock;
  992.         if ( lockpt ) {
  993.         flag = true;
  994.         error("can't remove locked revision %s",pt->num);
  995.         }
  996.             pt = pt->next;
  997.         }
  998.  
  999.         if ( ! flag ) {
  1000.             pt = strt;
  1001.             while( pt != tail ) {
  1002.         pt->selector = false;
  1003.         diagnose("deleting revision %s\n",pt->num);
  1004.                 pt = pt->next;
  1005.             }
  1006.         }
  1007.         return flag;
  1008. }
  1009.  
  1010.  
  1011.  
  1012.     static int
  1013. removerevs()
  1014. /*   Function:  get the revision range to be removed, and place the     */
  1015. /*              first revision removed in delstrt, the revision before  */
  1016. /*              delstrt in cuthead( nil, if delstrt is head), and the   */
  1017. /*              revision after the last removed revision in cuttail(nil */
  1018. /*              if the last is a leaf                                   */
  1019.  
  1020. {
  1021.     struct  hshentry *target, *target2, *temp;
  1022.     unsigned length;
  1023.     int flag;
  1024.  
  1025.         flag = false;
  1026.     if (!expandsym(delrev.strt, &numrev)) return 0;
  1027.     target = genrevs(numrev.string, (char*)nil, (char*)nil, (char*)nil, &gendeltas);
  1028.         if ( ! target ) return 0;
  1029.     if (cmpnum(target->num, numrev.string)) flag = true;
  1030.     length = countnumflds(numrev.string);
  1031.  
  1032.     if (delrev.code == 0) {  /*  -o  rev    or    -o  branch   */
  1033.         if (length & 1)
  1034.         temp=searchcutpt(target->num,length+1,gendeltas);
  1035.         else if (flag) {
  1036.         error("Revision %s doesn't exist.", numrev.string);
  1037.         return 0;
  1038.         }
  1039.         else
  1040.         temp = searchcutpt(numrev.string, length, gendeltas);
  1041.         cuttail = target->next;
  1042.             if ( branchpoint(temp, cuttail) ) {
  1043.                 cuttail = nil;
  1044.                 return 0;
  1045.             }
  1046.             delstrt = temp;     /* first revision to be removed   */
  1047.             return 1;
  1048.         }
  1049.  
  1050.     if (length & 1) {   /*  invalid branch after -o   */
  1051.         error("invalid branch range %s after -o", numrev.string);
  1052.             return 0;
  1053.         }
  1054.  
  1055.     if (delrev.code == 1) {  /*  -o  -rev   */
  1056.             if ( length > 2 ) {
  1057.                 temp = searchcutpt( target->num, length-1, gendeltas);
  1058.                 cuttail = target->next;
  1059.             }
  1060.             else {
  1061.                 temp = searchcutpt(target->num, length, gendeltas);
  1062.                 cuttail = target;
  1063.                 while( cuttail && ! cmpnumfld(target->num,cuttail->num,1) )
  1064.                     cuttail = cuttail->next;
  1065.             }
  1066.             if ( branchpoint(temp, cuttail) ){
  1067.                 cuttail = nil;
  1068.                 return 0;
  1069.             }
  1070.             delstrt = temp;
  1071.             return 1;
  1072.         }
  1073.  
  1074.     if (delrev.code == 2) {   /*  -o  rev-   */
  1075.             if ( length == 2 ) {
  1076.                 temp = searchcutpt(target->num, 1,gendeltas);
  1077.                 if ( flag)
  1078.                     cuttail = target;
  1079.                 else
  1080.                     cuttail = target->next;
  1081.             }
  1082.             else  {
  1083.                 if ( flag){
  1084.                     cuthead = target;
  1085.                     if ( !(temp = target->next) ) return 0;
  1086.                 }
  1087.                 else
  1088.                     temp = searchcutpt(target->num, length, gendeltas);
  1089.         getbranchno(temp->num, &numrev);  /* get branch number */
  1090.         target = genrevs(numrev.string, (char*)nil, (char*)nil, (char*)nil, &gendeltas);
  1091.             }
  1092.             if ( branchpoint( temp, cuttail ) ) {
  1093.                 cuttail = nil;
  1094.                 return 0;
  1095.             }
  1096.             delstrt = temp;
  1097.             return 1;
  1098.         }
  1099.  
  1100.         /*   -o   rev1-rev2   */
  1101.     if (!expandsym(delrev.end, &numrev)) return 0;
  1102.     if (
  1103.         length != countnumflds(numrev.string)
  1104.         ||    length>2 && compartial(numrev.string, target->num, length-1)
  1105.     ) {
  1106.         error("invalid revision range %s-%s", target->num, numrev.string);
  1107.             return 0;
  1108.         }
  1109.  
  1110.     target2 = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&gendeltas);
  1111.         if ( ! target2 ) return 0;
  1112.  
  1113.         if ( length > 2) {  /* delete revisions on branches  */
  1114.             if ( cmpnum(target->num, target2->num) > 0) {
  1115.         if (cmpnum(target2->num, numrev.string))
  1116.                     flag = true;
  1117.                 else
  1118.                     flag = false;
  1119.                 temp = target;
  1120.                 target = target2;
  1121.                 target2 = temp;
  1122.             }
  1123.             if ( flag ) {
  1124.                 if ( ! cmpnum(target->num, target2->num) ) {
  1125.             error("Revisions %s-%s don't exist.", delrev.strt,delrev.end);
  1126.                     return 0;
  1127.                 }
  1128.                 cuthead = target;
  1129.                 temp = target->next;
  1130.             }
  1131.             else
  1132.                 temp = searchcutpt(target->num, length, gendeltas);
  1133.             cuttail = target2->next;
  1134.         }
  1135.         else { /*  delete revisions on trunk  */
  1136.             if ( cmpnum( target->num, target2->num) < 0 ) {
  1137.                 temp = target;
  1138.                 target = target2;
  1139.                 target2 = temp;
  1140.             }
  1141.             else
  1142.         if (cmpnum(target2->num, numrev.string))
  1143.                     flag = true;
  1144.                 else
  1145.                     flag = false;
  1146.             if ( flag ) {
  1147.                 if ( ! cmpnum(target->num, target2->num) ) {
  1148.             error("Revisions %s-%s don't exist.", delrev.strt, delrev.end);
  1149.                     return 0;
  1150.                 }
  1151.                 cuttail = target2;
  1152.             }
  1153.             else
  1154.                 cuttail = target2->next;
  1155.             temp = searchcutpt(target->num, length, gendeltas);
  1156.         }
  1157.         if ( branchpoint(temp, cuttail) )  {
  1158.             cuttail = nil;
  1159.             return 0;
  1160.         }
  1161.         delstrt = temp;
  1162.         return 1;
  1163. }
  1164.  
  1165.  
  1166.  
  1167.     static void
  1168. updateassoc()
  1169. /*   Function: add or delete(if revno is nil) association    */
  1170. /*        which is stored in assoclst            */
  1171.  
  1172. {
  1173.     const struct Symrev *curassoc;
  1174.     struct  assoc   * pre,  * pt;
  1175.  
  1176.         /*  add new associations   */
  1177.         curassoc = assoclst;
  1178.         while( curassoc ) {
  1179.             if ( curassoc->revno == nil ) {  /* delete symbol  */
  1180.         pre = pt = Symbols;
  1181.                 while( pt && strcmp(pt->symbol,curassoc->ssymbol) ) {
  1182.             pre = pt;
  1183.             pt = pt->nextassoc;
  1184.         }
  1185.         if ( pt )
  1186.             if ( pre == pt )
  1187.             Symbols = pt->nextassoc;
  1188.             else
  1189.             pre->nextassoc = pt->nextassoc;
  1190.         else
  1191.             warn("can't delete nonexisting symbol %s",curassoc->ssymbol);
  1192.         }
  1193.         else if (expandsym(curassoc->revno, &numrev)) {
  1194.         /*   add symbol  */
  1195.         VOID addsymbol(fstrsave(numrev.string), curassoc->ssymbol, curassoc->override);
  1196.             }
  1197.             curassoc = curassoc->nextsym;
  1198.         }
  1199.  
  1200. }
  1201.  
  1202.  
  1203.  
  1204.     static void
  1205. updatelocks()
  1206. /* Function: remove lock for caller or first lock if unlockcaller is set;
  1207.  *           remove locks which are stored in rmvlocklst,
  1208.  *           add new locks which are stored in newlocklst,
  1209.  *           add lock for Dbranch or Head if lockhead is set.
  1210.  */
  1211. {
  1212.     const struct Lockrev *lockpt;
  1213.     struct hshentry *target;
  1214.  
  1215.     if (unlockcaller) { /*  find lock for caller  */
  1216.             if ( Head ) {
  1217.         if (Locks) {
  1218.             switch (findlock(true, &target)) {
  1219.               case 0:
  1220.             breaklock(Locks->delta); /* remove most recent lock */
  1221.             break;
  1222.               case 1:
  1223.             diagnose("%s unlocked\n",target->num);
  1224.             break;
  1225.             }
  1226.         } else {
  1227.             warn("No locks are set.");
  1228.         }
  1229.             } else {
  1230.         warn("can't unlock an empty tree");
  1231.             }
  1232.         }
  1233.  
  1234.         /*  remove locks which are stored in rmvlocklst   */
  1235.         lockpt = rmvlocklst;
  1236.         while( lockpt ) {
  1237.         if (expandsym(lockpt->revno, &numrev)) {
  1238.         target = genrevs(numrev.string, (char *)nil, (char *)nil, (char *)nil, &gendeltas);
  1239.                 if ( target )
  1240.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1241.             error("can't unlock nonexisting revision %s",lockpt->revno);
  1242.                    else
  1243.             breaklock(target);
  1244.                         /* breaklock does its own diagnose */
  1245.             }
  1246.             lockpt = lockpt->nextrev;
  1247.         }
  1248.  
  1249.         /*  add new locks which stored in newlocklst  */
  1250.         lockpt = newlocklst;
  1251.         while( lockpt ) {
  1252.         setlock(lockpt->revno);
  1253.             lockpt = lockpt->nextrev;
  1254.         }
  1255.  
  1256.     if (lockhead) {  /*  lock default branch or head  */
  1257.             if (Dbranch) {
  1258.         setlock(Dbranch);
  1259.         } else if (Head) {
  1260.         if (0 <= addlock(Head))
  1261.             diagnose("%s locked\n",Head->num);
  1262.             } else {
  1263.         warn("can't lock an empty tree");
  1264.             }
  1265.         }
  1266.  
  1267. }
  1268.  
  1269.  
  1270.  
  1271.     static void
  1272. setlock(const char *rev)
  1273. /* Function: Given a revision or branch number, finds the corresponding
  1274.  * delta and locks it for caller.
  1275.  */
  1276. {
  1277.         struct  hshentry *target;
  1278.  
  1279.     if (expandsym(rev, &numrev)) {
  1280.         target = genrevs(numrev.string, (char*)nil, (char*)nil,
  1281.                  (char*)nil, &gendeltas);
  1282.             if ( target )
  1283.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1284.             error("can't lock nonexisting revision %s", numrev.string);
  1285.                else
  1286.             if (0 <= addlock(target))
  1287.             diagnose("%s locked\n", target->num);
  1288.         }
  1289. }
  1290.  
  1291.  
  1292.  
  1293.     static void
  1294. rcs_setstate(const char *rev,const char *status)
  1295. /* Function: Given a revision or branch number, finds the corresponding delta
  1296.  * and sets its state to status.
  1297.  */
  1298. {
  1299.         struct  hshentry *target;
  1300.  
  1301.     if (expandsym(rev, &numrev)) {
  1302.         target = genrevs(numrev.string, (char*)nil, (char*)nil,
  1303.                  (char*)nil, &gendeltas);
  1304.             if ( target )
  1305.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1306.             error("can't set state of nonexisting revision %s to %s",
  1307.               numrev.string, status);
  1308.                else
  1309.                     target->state = status;
  1310.         }
  1311. }
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.     static int
  1318. buildeltatext(const struct hshentries *deltas)
  1319. /*   Function:  put the delta text on frewrite and make necessary   */
  1320. /*              change to delta text                                */
  1321. {
  1322.     int exit_stats;
  1323.     register FILE *fcut;    /* temporary file to rebuild delta tree */
  1324.     const char *cutfilename, *diffilename;
  1325.  
  1326.     cutfilename = nil;
  1327.     cuttail->selector = false;
  1328.     inittmpeditfiles();
  1329.     scanlogtext(deltas->first, false);
  1330.         if ( cuthead )  {
  1331.         cutfilename = maketemp(3);
  1332.         errno = 0;
  1333.             if ( (fcut = fopen(cutfilename, "w")) == NULL) {
  1334.         efaterror(cutfilename);
  1335.             }
  1336.  
  1337.         while (deltas->first != cuthead) {
  1338.         deltas = deltas->rest;
  1339.         scanlogtext(deltas->first, true);
  1340.             }
  1341.  
  1342.         finishedit((struct hshentry *)nil);
  1343.         arewind(fcopy);
  1344.         fastcopy(fcopy, fcut);
  1345.             swapeditfiles(false);
  1346.             ffclose(fcut);
  1347.         }
  1348.  
  1349.     while (deltas->first != cuttail)
  1350.         scanlogtext((deltas = deltas->rest)->first, true);
  1351.         finishedit((struct hshentry *)nil);    ffclose(fcopy);
  1352.  
  1353.         if ( cuthead ) {
  1354.         diffilename = maketemp(0);
  1355.             exit_stats = run((char*)nil,diffilename,
  1356.             DIFF DIFF_FLAGS, cutfilename, resultfile, (char*)nil);
  1357.         if (!WIFEXITED(exit_stats) || 1<WEXITSTATUS(exit_stats))
  1358.                 faterror ("diff failed");
  1359.         return putdtext(cuttail->num,curlogmsg,diffilename,frewrite,true);
  1360.     } else
  1361.         return putdtext(cuttail->num,curlogmsg,resultfile,frewrite,false);
  1362. }
  1363.  
  1364.  
  1365.  
  1366.     static void
  1367. buildtree()
  1368. /*   Function:  actually removes revisions whose selector field  */
  1369. /*        is false, and rebuilds the linkage of deltas.     */
  1370. /*              asks for reconfirmation if deleting last revision*/
  1371. {
  1372.     struct    hshentry   * Delta;
  1373.         struct  branchhead      *pt, *pre;
  1374.  
  1375.         if ( cuthead )
  1376.            if ( cuthead->next == delstrt )
  1377.                 cuthead->next = cuttail;
  1378.            else {
  1379.                 pre = pt = cuthead->branches;
  1380.                 while( pt && pt->hsh != delstrt )  {
  1381.                     pre = pt;
  1382.                     pt = pt->nextbranch;
  1383.                 }
  1384.                 if ( cuttail )
  1385.                     pt->hsh = cuttail;
  1386.                 else if ( pt == pre )
  1387.                     cuthead->branches = pt->nextbranch;
  1388.                 else
  1389.                     pre->nextbranch = pt->nextbranch;
  1390.             }
  1391.     else {
  1392.             if ( cuttail == nil && !quietflag) {
  1393.         if (!yesorno(false, "Do you really want to delete all revisions? [ny](n): ")) {
  1394.             error("No revision deleted");
  1395.             Delta = delstrt;
  1396.             while( Delta) {
  1397.             Delta->selector = true;
  1398.             Delta = Delta->next;
  1399.             }
  1400.             return;
  1401.         }
  1402.         }
  1403.             Head = cuttail;
  1404.     }
  1405.         return;
  1406. }
  1407.  
  1408. #if lint
  1409. /* This lets us lint everything all at once. */
  1410.  
  1411. const char cmdid[] = "";
  1412.  
  1413. #define go(p,e) {int p P((int,char**)); void e P((void)); if(*argv)return p(argc,argv);if(*argv[1])e();return 0;}
  1414.  
  1415.     int
  1416. main(int argc, char **argv)
  1417. {
  1418.     switch (argc) {
  1419.         case 0:    go(ciId,    ciExit);
  1420.         case 1:    go(coId,    coExit);
  1421.         case 2:    go(identId,    identExit);
  1422.         case 3:    go(rcsdiffId,    rdiffExit);
  1423.         case 4:    go(rcsmergeId,    rmergeExit);
  1424.         case 5:    go(rlogId,    rlogExit);
  1425.         case 6:    go(rcsId,    exiterr);
  1426.         default: return 0;
  1427.     }
  1428. }
  1429. #endif
  1430.